<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../assets/images/666.jpeg" alt="" />
      </div>
      <!-- 表单区域 -->
      <div class="login-form">
        <el-form
          label-width="80px"
          :model="loginFrom"
          :rules="loginFromRules"
          ref="loginformref"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              prefix-icon="iconfont icon-user"
              v-model="loginFrom.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              prefix-icon="iconfont icon-3702mima"
              show-password
              v-model="loginFrom.password"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 按钮区域 -->
      <div class="btns">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="success" @click="resetloginform">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      loginFrom: {
        username: "admin",
        password: "123456",
      },
      loginFromRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 重置
    resetloginform() {
      // console.log(this.$refs);
      this.$refs.loginformref.resetFields();
    },
    // 表单验证
    login() {
      this.$refs.loginformref.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginFrom);
        // console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error("error");
        }
        this.$message.success("success");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("home");
        // if (
        //   valid &&
        //   this.loginFrom.username == "admin" &&
        //   this.loginFrom.password == "123456"
        // ) {
        //   this.$message.success("success");
        // } else {
        //   this.$message.error("error");
        // }
        // window.sessionStorage.setItem("token", "zhangsan");
        // this.$router.push("home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background-color: #6ab04c;
  height: 100%;
  width: 100%;
  .login-box {
    height: 300px;
    width: 450px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar-box {
      height: 160px;
      width: 150px;
      // border: 1px solid #6ab04c;
      // border-radius: 50%;
      // padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login-form {
      width: 85%;
      position: absolute;
      bottom: 0;
      // margin-top: 50px;
      // display: flex;
      // align-items: flex-start;
    }
    .btns {
      display: flex;
      justify-content: space-around;
      margin-top: 100px;
    }
  }
}
</style>
